<?php namespace Admin\Controller; 

//商品列表模型
use \Common\Model\GoodsList;

// 商品货品控制器
class GoodsListController extends CommonController{

//	成员属性
	private $goodsListModel;


	//构造函数
	public function __init()
	{
		
//		实例化商品货品模型
		$this -> goodsListModel = new GoodsList;
		parent::__init();
	}
    //商品货品主页
    public function index(){
    	
		$gid = Q('get.gid',0,'intval');
//		将商品主键输出到前台
		View::with('gid',$gid);
//		var_dump($gid);die;
//		查询货品列表
		$goodsListData = Db::table('snshop_goods_list gl')
			-> where('snshop_goods_gid',$gid)
			-> get();
//		var_dump($goodsListData);die;
//		将字段 combine 转成数组
		foreach($goodsListData as $k => $v){
			$goodsListData[$k]['combine'] = explode(",", $v['combine']);
		}
//		var_dump($goodsListData);
		foreach($goodsListData as $k => $v){
			foreach($v['combine'] as $kk => $vv){
				$goodsListData[$k]['combine'][$kk] = Db::table('snshop_goods_attr')
													-> where('gtid',$vv)
													->pluck('gtvalue');
			}
		}		
//		var_dump($goodsListData);die;
//		var_dump($arrAttr);die;

//		分配到前台输出
		View::with('goodsListData',$goodsListData);
		
		
		
		
		
		
		$specData=Db::table('snshop_goods_attr ga')
		->join('snshop_type_attr ta','ga.taid','=','ta.taid')
		->where('ta.class',1)
		->where('ga.snshop_goods_gid',$gid)
//		因为添加的时候，对组合属性进行了升序排序，所以这里也要进行升序排序一下，不写SEC，代表默认
		->orderBy('ga.gtid')
		->groupBy('ta.taid')
		->get();
//		p($specData);die;
		foreach($specData as $k=>$v){
			$specData[$k]['sonData']=
			Db::table('snshop_goods_attr ga')
				->join('snshop_type_attr ta','ga.taid','=','ta.taid')
				->where('ta.class',1)
				->where('ga.snshop_goods_gid',$gid)
				->where('ta.taid',$v['taid'])
				->get();
		}
//		p($specData);die;
		View::with('specData',$specData);		
		

		
		
    	View::make();
    }
	
	
//	添加货品列表商品
	public function add(){
		if(IS_POST){
//			var_dump($_POST);die;
//			去模型中处理数据
			$result = $this -> goodsListModel -> store();
			if(!$result){
				View::error($this -> goodsListModel -> getError());
			}
			View::success('添加成功',U('index',array('gid' => $result)));
		}
	}
	
	
//	删除商品列表商品
	public function del(){

//		var_dump($_GET);die;
		$glid = Q('get.glid',0,'intval');
		$gid = Q('get.gid',0,'intval');
//		var_dump($glid);die;
//		从数据库获得数据，然后删除
		$result = $this -> goodsListModel -> where('glid',$glid) -> delete();
		if(!$result){
			View::error('删除失败');
		}
		View::success('删除成功',U('index',array('gid' => $gid)));
	}
	
//	编辑商品列表商品
	public function edit(){
		if(IS_POST){
//			提交给type模型处理数据
			$result = $this -> goodsListModel -> edit();
			
		}
	}
	
	
//	检索是否有重复的添加
	public function jiansun(){
//			var_dump($_POST);die;
			$gid = $_POST['gid'];
			$combineStr = rtrim($_POST['combineStr'],",");
//			echo $combineStr;die;		
//			查询货品表，看是否存在
			$combine = Db::table('snshop_goods_list') 
			-> where('snshop_goods_gid',$gid) 
			-> where('combine',$combineStr)
			-> pluck('combine');
			
			$arr = array('combine' => $combine);
			View::ajax($arr);
	}
	
	
	
	
	
	
}
?>